<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
     //1.移除数组 arr[1, 2, 3, 4, 2] 中的2。不要直接修改数组 arr，结果返回新的数组

          let arr=[1,2,3,4,2];
          let newArr=[]//定义空数组
          for(var i=0;i<arr.length;i++){
            if(arr[i]!=2){
              newArr.push(arr[i])//当i不等于2时添加元素
            }
          }
          
          console.log(newArr);
        
      //2.定义一个函数,能在数组 arr[1, 2, 3, 4, 2] 的 "2"后面添加元素 "Melon"。不要直接修改数组 arr，结果返回新的数组
         var arr=[1,2,3,4,2];
         function fn(arr,num,str){
             let newArr=[];
             for (let i = 0; i < arr.length; i++) {
                 if(arr[i]==num){
                     newArr.push(arr[i],str)
                 }else{
                     newArr.push(arr[i])
                 }
                 
             }return newArr
         } console.log(fn(arr,2,"melon"));
       
        
      //3.统计数组 arr[1, 2, 3, 4, 2] 中2出现的次数
           var arr=[1,2,3,4,2];
           var newArr=[];//空数组
           for(var i=0;i<=arr.length;i++){
            //遍历数组
            //   console.log(arr[i]);
              if(arr[i]==2){
                  newArr[newArr.length]=arr[i];
                  //如果元素等于2，则添加将数组arr[i]的值赋值给新数组
              }
           }console.log(newArr.length);//打印数组的长度=元素个数=出现的次数
      //4.在arr里面输出年龄小于17的对象
      var arr = [{
              name: "111",
              sex: "boy",
              age: 18
          },
          {
              name: "222",
              sex: "girl",
              age: 17
          },
          {
              name: "333",
              sex: "boy",
              age: 16
          },
          {
              name: "444",
              sex: "girl",
              age: 15
          },
          {
              name: "555",
              sex: "boy",
              age: 20
          }
      ];
      let newArr=[];
      for(let i=0;i<arr.length;i++){
         if(arr[i].age<17){
             newArr.push(arr[i]);
         }
      }console.log(newArr);


    
      //5.分析结果
     
        var b = "boy";
        console.log(b);//boy
        function fighting() {
          console.log(a);//undefined
          console.log(c);//undefined
          if (a === "apple") {
            a = "Alice";
          } else {
            a = "Ada";
          }
          console.log(a);//Ada
          var a = "Andy";
          middle();
          function middle() {
            console.log(c++);//NaN
            var c = 100;
            console.log(++c);//101
            small();
            function small() {
              console.log(a);//Andy
            }
          }
          var c = (a = 88);
          function bottom() {
            b = "baby";
            console.log(b);//baby
          }
          bottom();
        }
        fighting();
        console.log(b);//baby


        //因js中变量声明提升，函数声明提升
        function fighting() {
      function bottom() {
    b = "baby";
    console.log(b);//在函数体内局部变量b的值为"baby"
  }
      function middle() {
        function small() {
      console.log(a);//因函数small已调出且父级函数middle体被调用，局部变量a="andy"打印结果为andy
    }
        var c;
    console.log(c++);//局部变量c只声明未赋值与数字相加为NaN
     c = 100;
    console.log(++c);//变量c值为100，++c打印结果为101
    small();//调出函数
  }
      var a;
      var c;
  console.log(a);//局部变量a只声明未赋值打印结果undefined
  console.log(c);//局部变量c只声明未赋值打印结果undefined
  if (a === "apple") {
    a = "Alice";
  } else {
    a = "Ada";
  }
  console.log(a);//在函数体bottom中未见a="apple"的值所以打印Ada
   a = "Andy";
  middle();
   c = (a = 88);
  bottom();
}
var b = "boy";
console.log(b);//全局变量b的值为boy
fighting();
console.log(b);//调用函数体figting中的b值打印出来baby
    </script>
</body>
</html>